package com.example;

public class Main {
    public static class ArrayList {
        public int[] data;
        public int maxSize = 10;
        public int length;
    }

    public static void initArrayList(ArrayList L) {
        L.data = new int[L.maxSize];
        L.length = 0;
    }

    public static void increaseSize(ArrayList L, int len) {
        int[] tmp = L.data;
        L.data = new int[L.maxSize + len];
        for (int i = 0; i < L.length; ++i)
            L.data[i] = tmp[i];
        L.maxSize = L.maxSize + len;
    }

    public static void insert(ArrayList L, int index, int element) {
        if (index < 0) return;
        if (L.length == L.maxSize) increaseSize(L, L.maxSize); //空间满了 扩大一倍
        for (int i = L.length; i > index; --i) { //挪出一个位置插入
            L.data[i] = L.data[i - 1];
        }
        L.data[Math.min(index, L.length)] = element;
        ++L.length;
    }

    public static int delete(ArrayList L, int index) {
        if (index < 0 || index >= L.length) return -1;
        int del = L.data[index];
        for (int i = index; i < L.length - 1; ++i) { //后面元素往前挪
            L.data[i] = L.data[i + 1];
        }
        --L.length;
        return del;
    }

    public static boolean find(ArrayList L, int element) {
        for (int i = 0; i < L.length; ++i)
            if (L.data[i] == element) return true;
        return false;
    }

    public static int get(ArrayList L, int index) {
        if (index < 0 || index >= L.length) return -1;
        return L.data[index];
    }

    public static void printArray(ArrayList L) {
        for(int i = 0; i < L.length; ++i)
            System.out.print(L.data[i] + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        initArrayList(arrayList);
        insert(arrayList, 4, 1);
        insert(arrayList, 4, 2);
        insert(arrayList, 4, 3);
        insert(arrayList, 4, 4);
        insert(arrayList, 4, 5);
        insert(arrayList, 4, 6);
        printArray(arrayList);
    }
}
